package com.ibm.ba.demo.service;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibm.ba.demo.entity.SPSSModelConfiguration;
import com.ibm.ba.demo.exception.GeneralException;
import com.ibm.ba.demo.helper.BeanHelper;
import com.spss.scoring.schema.v2.jaxb.Input;
import com.spss.scoring.schema.v2.jaxb.RequestInputRow;
import com.spss.scoring.schema.v2.jaxb.RequestInputTable;
import com.spss.scoring.schema.v2.jaxb.ScoreRequest;

@Service
public class GeneralScoreRequestServiceImpl implements ScoreRequestService {

	private static Logger logger = Logger
			.getLogger(GeneralScoreRequestServiceImpl.class);

	@Autowired
	private SPSSModelConfiguration modelConfig;

	@Override
	public ScoreRequest convert(Map<String, Object> inputMap)
			throws GeneralException {
		logger.info("inputMap is:" + BeanHelper.beanToJson(inputMap));
		ScoreRequest scoreRequest = new ScoreRequest();
		scoreRequest.setId(modelConfig.getSpssModel());
		RequestInputTable rInputTable = new RequestInputTable();
		rInputTable.setName(modelConfig.getSpssTable());
		RequestInputRow rInputRow = new RequestInputRow();

		for (Entry<String, Object> entry : inputMap.entrySet()) {
			String name = entry.getKey().toString();
			String value = entry.getValue().toString();
			Input inputs = new Input();
			inputs.setName(name);
			inputs.setValue(value);
			rInputRow.getInput().add(inputs);
		}

		rInputTable.getRequestInputRow().add(rInputRow);
		scoreRequest.getRequestInputTable().add(rInputTable);
		logger.info("scoreRequest is:" + BeanHelper.beanToJson(scoreRequest));
		return scoreRequest;
	}

}
